package cn.merryyou.jpa.repository;

import cn.merryyou.jpa.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created on 2018/2/5.
 *
 * @author zlf
 * @since 1.0
 */
public interface BookRepository extends JpaRepository<Book, Integer> {
    /**
     * @desc 根据id查找book
     * @author ZZZ
     * @date 2018/3/18 018
     * @version 1.0
     */
    @Query("select b from Book b where b.id = ?1")
    Book findBookById(Integer id);
    /**
     * @desc 根据name查找book
     * @author ZZZ
     * @date 2018/3/18 018
     * @version 1.0
     */
    List<Book> findBooksByName(String name);
    /**
     * @desc 根据name和author查找book
     * @author ZZZ
     * @date 2018/3/18 018
     * @version 1.0
     */
    List<Book> findBooksByNameAndAuthor(String name, String author);
    /**
     * @desc 根据id更新书名
     * @author ZZZ
     * @date 2018/3/18 018
     * @version 1.0
     */
    @Transactional
    @Modifying
    @Query("update Book b set b.name = ?2 where b.id = ?1")
    int updateBookNameById(Integer id, String name);
    /**
     * @desc 根据id更新作者
     * @author ZZZ
     * @date 2018/3/18 018
     * @version 1.0
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Modifying
    @Query("update Book b set b.author = ?2 where b.id = ?1")
    int updateBookAuthorById(Integer id, String author);
}
